<?php

namespace app\admin\model\service\shop;

use think\Model;
use traits\model\SoftDelete;

class Goods extends Model
{

    use SoftDelete;

    

    // 表名
    protected $name = 'service_goods';
    
    // 自动写入时间戳字段
    protected $autoWriteTimestamp = 'integer';

    // 定义时间戳字段名
    protected $createTime = 'createtime';
    protected $updateTime = 'updatetime';
    protected $deleteTime = 'deletetime';

    // 追加属性
    protected $append = [
        'type_text',
        'to_shop_text',
        'is_travel_text',
        'spec_type_text',
        'choose_skill_type_text',
        'shop_state_text',
        'status_text'
    ];
    

    protected static function init()
    {
        self::afterInsert(function ($row) {
            $pk = $row->getPk();
            $row->getQuery()->where($pk, $row[$pk])->update(['weigh' => $row[$pk]]);
        });
    }

    
    public function getTypeList()
    {
        return ['0' => __('Type 0'), '1' => __('Type 1')];
    }

    public function getToShopList()
    {
        return ['door' => __('To_shop door'), 'shop' => __('To_shop shop')];
    }

    public function getIsTravelList()
    {
        return ['0' => __('Is_travel 0'), '1' => __('Is_travel 1')];
    }

    public function getSpecTypeList()
    {
        return ['0' => __('Spec_type 0'), '1' => __('Spec_type 1')];
    }

    public function getChooseSkillTypeList()
    {
        return ['0' => __('Choose_skill_type 0'), '1' => __('Choose_skill_type 1')];
    }

    public function getShopStateList()
    {
        return ['0' => __('Shop_state 0'), '1' => __('Shop_state 1'), '-1' => __('Shop_state -1')];
    }

    public function getStatusList()
    {
        return ['normal' => __('Status normal'), 'hidden' => __('Status hidden')];
    }


    public function getTypeTextAttr($value, $data)
    {
        $value = $value ? $value : (isset($data['type']) ? $data['type'] : '');
        $list = $this->getTypeList();
        return isset($list[$value]) ? $list[$value] : '';
    }


    public function getToShopTextAttr($value, $data)
    {
        $value = $value ?: ($data['to_shop'] ?? '');
        $valueArr = explode(',', $value);
        $list = $this->getToShopList();
        return implode(',', array_intersect_key($list, array_flip($valueArr)));
    }


    public function getIsTravelTextAttr($value, $data)
    {
        $value = $value ? $value : (isset($data['is_travel']) ? $data['is_travel'] : '');
        $list = $this->getIsTravelList();
        return isset($list[$value]) ? $list[$value] : '';
    }


    public function getSpecTypeTextAttr($value, $data)
    {
        $value = $value ? $value : (isset($data['spec_type']) ? $data['spec_type'] : '');
        $list = $this->getSpecTypeList();
        return isset($list[$value]) ? $list[$value] : '';
    }


    public function getChooseSkillTypeTextAttr($value, $data)
    {
        $value = $value ? $value : (isset($data['choose_skill_type']) ? $data['choose_skill_type'] : '');
        $list = $this->getChooseSkillTypeList();
        return isset($list[$value]) ? $list[$value] : '';
    }


    public function getShopStateTextAttr($value, $data)
    {
        $value = $value ? $value : (isset($data['shop_state']) ? $data['shop_state'] : '');
        $list = $this->getShopStateList();
        return isset($list[$value]) ? $list[$value] : '';
    }


    public function getStatusTextAttr($value, $data)
    {
        $value = $value ? $value : (isset($data['status']) ? $data['status'] : '');
        $list = $this->getStatusList();
        return isset($list[$value]) ? $list[$value] : '';
    }

    protected function setToShopAttr($value)
    {
        return is_array($value) ? implode(',', $value) : $value;
    }


}
